var map;
var myOptions;
var locations;

function mapMarker(mapDiv, myLatlng, locations) {

	var myOptions = {
		zoom : 10,
		center : myLatlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		disableDefaultUI : false,
		scrollwheel : true,
		draggable : true,
		navigationControl : true,
		mapTypeControl : false,
		scaleControl : true,
		disableDoubleClickZoom : false
	};

	var map = new google.maps.Map($(mapDiv)[0], myOptions);

	var infowindow = new google.maps.InfoWindow();

	var marker, i;

	for (i = 0; i < locations.length; i++) {
		var lat = locations[i][1];
		var log = locations[i][2];

		marker = new google.maps.Marker({
			position : new google.maps.LatLng(lat, log),
			map : map
		});

		// insere um evento no click do marker para abrir o tooltip
		google.maps.event.addListener(marker, 'click', (function(marker, i) {
			return function() {
				infowindow.setContent(locations[i][0]);
				infowindow.open(map, marker);
			}
		})(marker, i));
	}
};

// https://developers.google.com/maps/documentation/javascript/examples/layer-heatmap
function mapHeater(mapDiv, myLatlng, locations, _zoom) {
	// sorry - this demo is a beta
	// there is lots of work todo
	// but I don't have enough time for eg redrawing on dragrelease right now
	var myOptions = {
		zoom : _zoom,
		center : myLatlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		disableDefaultUI : false,
		scrollwheel : true,
		draggable : true,
		navigationControl : true,
		mapTypeControl : false,
		scaleControl : true,
		disableDoubleClickZoom : false
	};

	map = new google.maps.Map($(mapDiv)[0], myOptions);

	heatmap = new HeatmapOverlay(map, {
		"radius" : 15,
		"visible" : true,
		"opacity" : 60
	});

	google.maps.event.addListenerOnce(map, "idle", function() {
		heatmap.setDataSet(locations);
	});

};

/*
 * Utilizacao da biblioteca HeatMap do Google
 * https://developers.google.com/maps/tutorials/visualizing/earthquakes?hl=pt-BR
 */
function mapHeaterUS14(mapDiv, myLatlng, locations, _zoom) {
	// Configuracao do Mapa
	var myOptions = {
		zoom : _zoom,
		center : myLatlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		disableDefaultUI : false,
		scrollwheel : true,
		draggable : true,
		navigationControl : true,
		mapTypeControl : false,
		scaleControl : true,
		disableDoubleClickZoom : false
	};

	map = new google.maps.Map($(mapDiv)[0], myOptions);

	// Monta array com os pontos de localizacao do HeatMap
	var data = [];
	for (i = 0; i < locations.length; i = i + 1) {
		var lat = locations[i].localidade_latitude;
		var log = locations[i].localidade_longitude;
		//var type = locations[i][3];
		
		data.push(new google.maps.LatLng(lat, log));
	}

	// Transforma o Array para um tipo MVCArray
	var pointArray = new google.maps.MVCArray(data);

	heatmap = new google.maps.visualization.HeatmapLayer({
		data : pointArray
	});

	heatmap.setMap(map);

}


// http://jsfiddle.net/mG4uz/14/
function mapMarkerUS17(mapDiv, myLatlng, locations, _zoom) {
	/* opcoes de formatacao do map */
	myOptions = {
		zoom : _zoom,
		center : myLatlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		disableDefaultUI : false,
		scrollwheel : true,
		draggable : true,
		navigationControl : true,
		mapTypeControl : false,
		scaleControl : true,
		disableDoubleClickZoom : false
	};

	map = new google.maps.Map($(mapDiv)[0], myOptions);

	/* laco para montar todos os locations */
	for (i = 0; i < locations.length; i = i + 1) {
		var lat = locations[i][1];
		var log = locations[i][2];
		var type = locations[i][3];

		try {
			var marker = new google.maps.Marker({
				position : new google.maps.LatLng(lat, log),
				map : map,
				icon : getImage(type)
			});

			var infowindow = new google.maps.InfoWindow();
			google.maps.event.addListener(marker, 'click',
					(function(marker, i) {
						return function() {
							var contentTooltip = '<b>ID_Location:</b> '
									+ locations[i][0] + '<br><b>Tipo:</b> '
									+ locations[i][3]
									+ '<br><b>Valor Transacao:</b> R$ '
									+ locations[i][4];
							infowindow.setContent(contentTooltip);
							infowindow.open(map, marker);
						}
					})(marker, i));

		} catch (err) {
			// alert(err);
		}

	}

};

/*
 * Pega o tipo de imagem conforme o tipo de fraude carrega bibliotecas de
 * marcadores (cores diferentes para tipos distintos)
 */
function getImage(type) {
	var out;
	switch (type) {
	case "ESTELIONATO":
		out = {
			url : 'images/flag1.png'
		};
		break;
	case "FRAUDE-AMIGA":
		out = {
			url : 'images/flag2.png'
		};
		break;
	case "AUTO-FRAUDE":
		out = {
			url : 'images/flag3.png'
		};
		break;	
	default:
		out = {
			url : 'images/flag4.png'
		};
	}
	return out;
}

/*
 * funcao que usa Linq para identificar os tipos da locations e gera a legenda
 * de tipos na lateral
 */
function identifyTypes(locations) {
	ClearDiv();

	// http://linqjs.codeplex.com/
	var temp = Enumerable.From(locations).Select("$[3]").ToArray();

	var types = Enumerable.From(temp).Distinct().ToArray();

	// alert(types);

	var parent = document.getElementById('listTypes');
	var listview = document.createElement('ul');
	listview.setAttribute('id', 'listview');

	for ( var i = 0; i < types.length; i++) {
		var listItem = document.createElement('li');
		listItem.setAttribute('id', 'listitem' + types[i]);
		listItem.innerHTML = 'Fraude tipo ' + types[i];

		var img = document.createElement('img');
		var linkImg = getImage(types[i]).url;
		img.setAttribute('src', linkImg);
		listItem.appendChild(img);

		listview.appendChild(listItem);
	}
	parent.appendChild(listview);
}


/* funcao que limpa a Div de Tipos */
function ClearDiv() {
	$("#listTypes").empty();
}